#!/bin/sh
#
# Start & stop the inadyn client
#

mounts() {
  mkdir -p /tmp/inadyn
  cd /chroot/inadyn && \
    mount -o bind /usr/lib usr/lib && \
    mount -o bind /usr/sbin usr/sbin && \
    mount -o bind /lib lib && \
    mount -o bind /etc etc && \
    mount -t tmpfs -o size=1048576 none tmp && \
    mkdir tmp/dev && \
    mknod tmp/dev/urandom c 1 9
    chmod 666 tmp/dev/urandom
}

umounts() {
    umount /chroot/inadyn/usr/lib
    umount /chroot/inadyn/lib
    umount /chroot/inadyn/tmp
}

start() {
    cd /chroot/inadyn
    for conf in tmp/configs/*.conf; do
      if [ -e "$conf" ]; then
        babysit 120 chroot /chroot/inadyn \
          inadyn --input_file /"$conf" --cache-dir /tmp --pidfile /tmp/inadyn.pid \
          --change_persona nobody:nogroup 2>&1 | logos dyndns &
      fi
    done
}

stop() {
    pkillwait -f '(babysit.*)inadyn'
    pkillwait -x inadyn
}

case "$1" in
        start)
                mounts
                start
                ;;
        stop)
                stop
                umounts
                ;;
        restart)
                # Don't unmount and remount
                stop
                start
                ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
                exit 1
esac

exit $?
